package com.example.dragcontentviewdemo.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.util.Log;
import android.view.MotionEvent;

import android.widget.Toast;
import androidx.annotation.Nullable;

import com.example.dragcontentviewdemo.R;
import com.example.dragcontentviewdemo.activity.ui.adapter.DemoSimpleListViewAdapter;
import com.example.dragcontentviewdemo.activity.ui.view.DemoListView;
import com.example.dragcontentviewdemo.helper.DraggableActivityController;
import com.example.dragcontentviewdemo.helper.protocols.DraggableActivityGestureListener;
import java.util.Objects;

public class ChildActivity extends Activity {
    private static final String TAG = "ChildActivity";
    Handler handler = new Handler(Looper.getMainLooper());
    DemoListView listView = null;
    DraggableActivityController controller;

    //some test value
    private int taskId = -1;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.child_layout);
        configure();
        taskId = getTaskId();
        Log.d(TAG, "onCreate: this taskId = "+ taskId);
        Log.d(TAG, "onCreate: is task root = " + isTaskRoot());
        Log.d(TAG,"windowManager: " + Objects.equals(getWindowManager(), MainActivity.windowManager));

        /*
        简单测试
         */
        Message message = Message.obtain(handler, () -> {
            controller = DraggableActivityController.obtain(ChildActivity.this,null);
            controller.setCallback(new DraggableActivityGestureListener() {
                @Override
                public void onMoved(float fraction, boolean ltr) {

                }

                @Override
                public void onMoveOutOfBound() {
                    makeToast("进入后台，并唤起主活动");
                    //moveTaskToBack(true); //非根Activity不会进入后台
                    Intent resumeMainIntent = new Intent(ChildActivity.this,MainActivity.class);
                    //resumeMainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(resumeMainIntent);
                    overridePendingTransition(0,R.anim.activity_fade_out);
                }

                @Override
                public boolean isInterestedIn(MotionEvent event) {
                    return listView.isInterestedIn(event);
                }
            });
            controller.setVelocityXThreshold(10000);
        });
        message.sendToTarget();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: this taskId = "+ taskId);
        makeToast("onResume 显示UI "+ "子活动进程pid = " + Process.myPid() + "任务id: " + taskId);
    }

    private void configure(){
        listView = findViewById(R.id.listView);
        DemoSimpleListViewAdapter adapter = new DemoSimpleListViewAdapter(20);
        listView.setAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: this taskId = "+ taskId);
        makeToast("onDestroy");
    }

    private void makeToast(String msg){
        StringBuilder sb = new StringBuilder("子活动");
        sb.append(" ").append(msg);
        Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show();
    }
}
